# -*- coding: UTF-8 -*-
import xlrd


def read_execl(path):
    info = []  # 最后的数据为[{'case_name': 'login_case_001', 'describe': '用户输入正确的用户名、密码、\n验证码登录成功', 'test_type': 'GUI',
    data = xlrd.open_workbook(path)
    sheet_names = data.sheet_names()  # 获取excel所有sheet名集合形如：['sheet1', 'sheet2', 'sheet3']
    for sheet_name in sheet_names:
        sheet = data.sheet_by_name(sheet_name)  # 用名字指定sheet页
        nrow = sheet.nrows  # 获取表格行数
        for row in range(2, nrow):  # 对知道行数的execl表做字典处理
            dict = {}
            dict['system'] = sheet.cell(row, 1).value.replace("\n", "")  # 从2开始取2，1的数据 （3行第2列的数据）
            dict['describe'] = sheet.cell(row, 2).value.replace("\n", "")  # .replace("\n", "") 去掉表格中的\n 换行符
            dict['title'] = sheet.cell(row, 3).value.replace("\n", "")
            dict['request'] = {'url': sheet.cell(row, 4).value, 'method': sheet.cell(row, 5).value}
            dict['headers'] = eval(sheet.cell(row, 6).value.replace("\n", ""))
            dict['json'] = eval(sheet.cell(row, 7).value.replace("\n", ""))  # 字符串转化为字典
            dict['response_extraction'] = eval(sheet.cell(row, 8).value.replace("\n", ""))
            # dict['relation'] = eval(sheet.cell(row, 9).value.replace("\n", ""))
            dict['expected'] = eval(sheet.cell(row, 9).value.replace("\n", ""))
            info.append(dict)

    return info


if __name__ == '__main__':
    print(read_execl())
